﻿'***********************************************************************
' Assembly         : SOFA.Caching
' Copyright        : (c) Jebarson. All rights reserved.
' Website			: http://www.jebarson.info
'***********************************************************************
Option Strict Off
Imports System.Collections
Imports System.Globalization

Namespace Caching.StaticStore
    ''' <summary>
    ''' Factory for static storage.
    ''' </summary>
    Friend NotInheritable Class StaticStorageFactory
        Implements IStorageFactory

#Region " Declarations "
        ''' <summary>
        ''' Member variable for the storage ware house.
        ''' </summary>
        Private Shared wareHouse As List(Of Object)

        ''' <summary>
        ''' Member variable for the singleton instance.
        ''' </summary>
        Private Shared instanceValue As New StaticStorageFactory
#End Region
        
#Region " Constructors "
        ''' <summary>
        ''' Initializes a new instance of the <see cref="StaticStorageFactory" /> class.
        ''' </summary>
        Private Sub New()

            wareHouse = New List(Of Object)
        End Sub
#End Region

#Region " Public Properties "
        ''' <summary>
        ''' Gets the singleton instance.
        ''' </summary>
        ''' <value>The singleton instance.</value>
        Public Shared ReadOnly Property Instance() As StaticStorageFactory
            Get
                If (instanceValue Is Nothing) Then
                    instanceValue = New StaticStorageFactory()
                End If

                Return instanceValue
            End Get
        End Property
#End Region

#Region " Friend Methods "
        ''' <summary>
        ''' Adds the storage item.
        ''' </summary>
        ''' <param name="item">The storage item.</param>
        Public Sub AddStorageItem(ByVal item As Object) Implements IStorageFactory.AddStorageItem

            If Me.GetStorageItem(item.Name) IsNot Nothing Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "Storage {0} already exists.", item.Name))
            End If

            wareHouse.Add(item)
        End Sub

        ''' <summary>
        ''' Gets the storage item.
        ''' </summary>
        ''' <param name="name">The name.</param>
        ''' <returns>The storage item.</returns>
        Public Function GetStorageItem(ByVal name As String) As Object Implements IStorageFactory.GetStorageItem

            If (wareHouse.Where(Function(item As Object) item.Name = name).Count > 0) Then
                Return wareHouse.Where(Function(item As Object) item.Name = name).ElementAt(0)
            End If

            Return Nothing
        End Function

        ''' <summary>
        ''' Gets the name.
        ''' </summary>
        ''' <param name="item">The item.</param>
        ''' <returns>The name of the storage item.</returns>
        Public Function GetName(ByVal item As Object) As String Implements IStorageFactory.GetName

            If (item Is Nothing) Then
                Throw New InvalidOperationException("item cannot be null.")
            End If
            Return item.GetType.ToString
        End Function

        ''' <summary>
        ''' Gets the storage items.
        ''' </summary>
        ''' <returns>The list of storage items.</returns>
        Public Function GetStorageItems() As List(Of Object) Implements IStorageFactory.GetStorageItems

            Return wareHouse
        End Function
#End Region

    End Class
End Namespace
